package cn.maxhou.controller;

import cn.max.anno.RequestMapping;
import cn.maxhou.entity.User;
import cn.maxhou.mapper.UserMapper;
import cn.maxhou.service.UserService;
import cn.maxhou.service.impl.UserServiceImpl;
import cn.maxhou.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

public class UserController {
    @RequestMapping(value = "/zcuser")
    public void zcuser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");

        int flag=0;

        PrintWriter out = resp.getWriter();

        String username = req.getParameter("username");
        String paasowrd = req.getParameter("paasowrd");
        String cardNo = req.getParameter("cardNo");
        String Tel = req.getParameter("Tel");
        String address = req.getParameter("address");
        String postNumber = req.getParameter("postNumber");


        User user=new User();
        user.setUserName(username);
        user.setUserPassword(paasowrd);
        user.setUserCardNo(cardNo);
        user.setUserTel(Tel);
        user.setUserAddress(address);
        user.setUserPostNumber(postNumber);

        UserService userService=new UserServiceImpl();

        if (userService.checkusername(username)==0&(!username.isEmpty() & !paasowrd.isEmpty())) {
            flag=userService.insert(user);
        }

        if (flag==1) {
            out.println("注册成功");
        }else {
            out.println("注册失败");
        }
    }

    @RequestMapping(value = "/checkusername")
    public void checkusername(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");

        PrintWriter out = resp.getWriter();
        String username = req.getParameter("username");

        System.out.println(username);

        UserService userService=new UserServiceImpl();


        int flag = userService.checkusername(username);
        System.out.println(flag);
        out.print(flag);
    }

    @RequestMapping(value = "/user/login")
    public String userlogin(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        UserService userService=new UserServiceImpl();
        PrintWriter out = resp.getWriter();

        if(!username.isEmpty()) {
            if (userService.checkusername(username)==1){
                if (userService.isUser(username)==1){
                    if (userService.userlogin(username,password)==1){
                        User user=userService.getUser(username);
                        req.getSession().setAttribute("user",user);
                        return "forward:/do/getAuctionList";
                    }else {
                        out.println("密码错误,登录失败");
                    }
                }else {
                    out.print("用户类型不是普通用户,请管理员进入管理员登录页面登录");
                }
            }else {
                out.print("用户名不存在,登录失败");
            }
        }
        return null;
    }

    @RequestMapping(value = "/admin/login")
    public String adminlogin(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        UserService userService=new UserServiceImpl();
        PrintWriter out = resp.getWriter();

        if(!username.isEmpty()) {
            if (userService.checkusername(username)==1){
                if (userService.isAdmin(username)==1){
                    if (userService.userlogin(username,password)==1){
                        User user=userService.getUser(username);
                        req.getSession().setAttribute("user",user);
                        return "forward:/do/getAuctionList";
                    }else {
                        out.println("密码错误,登录失败");
                    }
                }else {
                    out.print("用户类型不是管理员,请普通用户进入普通用户登录页面登录");
                }
            }else {
                out.print("用户名不存在,登录失败");
            }
        }
        return null;
    }
}
